var curIndx = 0;
var myChart;
var stations=[];
var cityType=[];
var mapType = [
               {dataIndex: 0, name: 'china',value:0}
               ];


//站点位置，容量
function getStations(){
	$.ajax({
		type: "GET",
		url: "/smartLNG/api/"+"staNum/findAllSiteGoe",
		async:false,
		dataType:"text",
		success: function(data) {
			var stationsData = eval("[" + data + "]");

			for (var i in stationsData){
				stations.push(stationsData[i]);
			}
		},
		error: function (e) {
		}
	});
}

//得到省市的站点数量
function getVal(){
	$.ajax({
		type: "GET",
		url: "/smartLNG/api/"+"staNum/findStaNumByCt",
		async:false,
		dataType:"text",
		success: function(data) {
			var stationsVal = eval("[" + data + "]");

			for (var i in stationsVal){
				mapType.push(stationsVal[i]);
			}
		},
		error: function (e) {
		}
	});
}


//统计导出
function downLoad(){
	var downLoadlen = mapType.length;
	var downLoadmt = mapType[curIndx % downLoadlen].name;
	var outData=[];
	var title=[];
	var inData=[];

	if(downLoadmt=='china'){
		outData=[['中国全部省','站点数量（个）']];
		title="中国各省站点分布";
		inData=[];
		inData=getData(mapType);
		commonDownLoad(title,outData,inData);
	}else{
		outData=[[downLoadmt,'站点数量']];
		title=downLoadmt+"各市站点分布";
		inData=[];
		inData=getData(cityType);
		commonDownLoad(title,outData,inData);
	}
}


function getData(oldData){
	var data=[];
	var ndata=[];
	var vdata=[];
	for(var i=0;i<oldData.length;i++){
		ndata.push(oldData[i].name);
		vdata.push(oldData[i].value);
	}
	data.push(ndata);
	data.push(vdata);
	return data;
}

$(document).ready(function() {
	getStations();
	getVal();
	var xh=document.documentElement.clientHeight ;
	$('#map').css({
		'height':xh
	});
	var xw=document.documentElement.clientWidth ;
	$('#map').css({
		'width':xw
	});
	myChart = echarts.init(document.getElementById("map"));
//	var h=document.documentElement.clientHeight ;
//	$('#map').css({
//		'height':h
//	});
	myChart.setOption(option, true);



	myChart.on(echarts.config.EVENT.MAP_SELECTED, function(param) {
		var len = mapType.length;
		var mt = mapType[curIndx % len].name;
		if (mt == 'china') {
			// 全国选择时指定到选中的省份
			var selected = param.selected;
			for ( var i in selected) {
				if (selected[i]) {
					mt = i;
					while (len--) {
						if (mapType[len].name == mt) {
							curIndx = len;
						}
					}
					break;
				}
			}
			option.tooltip.formatter =function(params) {//maptype
				var res = params.name+'<br/>';
				for (var i = 0; i < mapType.length; i++) {
					if(mapType[i].name==params.name){
						res+='站点数量 : '+mapType[i].value;
					}
				}
				return res;
			} ;
			//去拿mt(省) 每个市的value, 返回cityType
			$.ajax({
				type: "GET",
				url: "/smartLNG/api/"+"staNum/findStaNumByP",
				async:false,
				dataType:"text",
				data: "province="+encodeURI(mt),
				success: function(data) {
					var stationsVal = eval("[" + data + "]");

					for (var i in stationsVal){
						cityType.push(stationsVal[i]);
					}

				},
				error: function (e) {
				}
			});

			option.tooltip.formatter =function(params) { //citytype
				var res = params.name+'<br/>';
				for (var i = 0; i < cityType.length; i++) {
					if(cityType[i].name==params.name){
						res+='站点数量 : '+cityType[i].value;
					}
				}
				return res;
			} ;
		}else {
			curIndx = 0;
			mt = 'china';
			option.tooltip.formatter =function(params) {
				var res = params.name+'<br/>';
				for (var i = 0; i < mapType.length; i++) {
					if(mapType[i].name==params.name){
						res+='站点数量 : '+mapType[i].value;
					}
				}
				cityType=[];
				return res;
			} ;
		}
		option.series[0].mapType = mt;
		myChart.setOption(option, true);
	});
});

var option = {
		backgroundColor: '#1b1b1b',
//		title : {
//			text: '全国气站分布',
//			x:'center',
//			textStyle : {
//				color: '#fff'
//			}
//		},

		tooltip : {
			trigger: 'item',
			formatter: function(params) {
				var res = params.name+'<br/>';
				for (var i = 0; i < mapType.length; i++) {
					if(mapType[i].name==params.name){
						res+='站点数量 : '+mapType[i].value;
					}
				}
				return res;
			}
		},
		 toolbox: {
		        show : true,
		        orient: 'horizontal',
		        x: 'left',
		        y: 'center',
		        itemSize:50,
		        feature : {
		        	   myTool : {
		                   show : true,
		                   title : '统计导出',
		                   icon : '../../images/statistic-map.png',
		                   onclick : function (){
		                	   downLoad();
		                   }
		                },
		        }
		    },
		dataRange: {
			splitList: [
			            {start: 120, end: 180,label: '一级加气站（120-180）'},
			            {start: 60, end: 120,label: '二级加气站（60-120）'},
			            {start: 0, end: 60,label: '三级加气站（0-60）'}
			            ],
			            color:['#1e90ff','#f0ffff'],
			            textStyle : {
			                color: '#fff'
			            }
		},
		series : [
		          {
		        	  name: '气站分布',
		        	  type: 'map',
		        	  mapType: 'china',
		        	  selectedMode : 'single',
		        	  itemStyle:{
		        		  normal:{
		        			  borderColor:'rgba(100,149,237,1)',
		        			  borderWidth:1.5,
		        			  areaStyle:{
		        				  color: '#1b1b1b'
		        			  }
		        		  },
		        		  emphasis:{label:{show:true}}
		        	  },
		        	  mapLocation: {
		                  x: 'center',
		                  y: 'center',
		                  width: '50%'
		              },
		        	  data:[],
                markPoint  : {
//		        		  effect:{show:true},
		        		  symbolSize: 5,
		        		  itemStyle: {
		        			  normal: {
		        				  borderColor: '#87cefa',
		        				  borderWidth: 1.5,            // 标注边线线宽，单位px，默认为1
		        				  label: {
		        					  show: false
		        				  }
		        			  },
		        			  emphasis: {
		        				  borderColor: '#1e90ff',
		        				  borderWidth: 5,
		        				  label: {
		        					  show: false
		        				  }
		        			  }
		        		  },
		        		  data :stations
		        	  }
		          }
		          ]
};
